#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;
const int N = 1010;

int n, m, idx;
int tr[N][4], cnt[N];
int ne[N], q[N];
int f[N][N];
char str[N];

int get(char c){
    if(c == 'A') return 0;
    if(c == 'G') return 1;
    if(c == 'C') return 2;
    return 3;
}

void insert(){
    int p = 0;
    for(int i = 0; str[i]; i ++){
        int t = get(str[i]);
        if(!tr[p][t]) tr[p][t] = ++idx;
        p = tr[p][t];
    }
    cnt[p] = 1;
}


void build(){
    int hh = 0, tt = -1;
    for(int i = 0; i < 4; i ++){
        if(tr[0][i]) q[++tt] = tr[0][i];
    }

    while(hh <= tt){
        int p = q[hh++];

        for(int i = 0; i < 4; i ++){
            int t = tr[p][i];
            if(!t){
                tr[p][i] = tr[ne[p]][i];
            }else{
                ne[t] = tr[ne[p]][i];
                cnt[t] |= cnt[tr[ne[p]][i]];
                q[++tt] = t;
            }
        }
    }
}

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);

    int T = 1;
    while(scanf("%d", &n), n){
        memset(tr, 0, sizeof tr);
        memset(cnt, 0, sizeof cnt);
        memset(ne, 0, sizeof ne);
        idx = 0;

        for(int i = 0; i < n; i ++){
            scanf("%s", str);
            insert();
        }

        build();

        memset(f, 0x3f, sizeof f);
        f[0][0] = 0;
        scanf("%s", str + 1);
        m = strlen(str + 1);

        for(int i = 0; i < m; i ++){
            for(int j = 0; j <= idx; j ++){
                for(int k = 0; k < 4; k ++){
                    int cost = (get(str[i + 1]) != k);

                    int p = tr[j][k];
                    if(!cnt[p]) f[i + 1][p] = min(f[i + 1][p], f[i][j] + cost);
                }
            }
        }

        int res = 0x3f3f3f3f;
        for(int i = 0; i <= idx; i ++){
            res = min(res, f[m][i]);
        }
        if(res != 0x3f3f3f3f){
            printf("Case %d: %d\n", T++, res);
        }else{
            printf("Case %d: %d\n", T++, -1);
        }
    }

    return 0;
}